/******************************************************************
 *
 *    中软科
 *
 *    Copyright (c) 2016-2020
 *
 *    Package:     io.finer.erp.jeecg.bas.util
 *
 *    Filename:    GetCuttingMethod.java
 *
 *    Description: 计算智能分切方案
 *
 *    Copyright:   Copyright (c) 2016-2020
 *
 *    Company:     山东中软科互联网科技有限公司
 *
 *    @author:     duanyong
 *
 *    @version:    1.0.0
 *
 *    Create at:   2020-09-04 14:41:36
 *
 *    Revision:
 *
 *    2020-09-04 14:41:36 - first revision by duanyong
 *
 *    Function List:
 *
 *****************************************************************/

package io.finer.erp.jeecg.bas.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jeecg.common.util.TestDTO;

import cn.hutool.core.math.MathUtil;
import io.finer.erp.jeecg.bas.entity.CuttingMethod;

/**
 * @ClassName GetCuttingMethod
 * @Description 计算智能分切方案
 * @author duanyong
 * @Date 2020-09-04 14:41:36
 * @version 1.0.0
 */

public class GetCuttingMethod {
	/**
	 * @Description 输出智能分切方案
	 * @param length 需要的物料长度
	 * @param dtoList 物料集合
	 * @param widths 需要的宽
	 * @return
	 * Create at: 2020-09-04 14:43:56
	 * @author: duanyong
	 * Revision:
	 *    2020-09-04 14:43:56 - first revision by duanyong
	 *        
	 */
	public static List<CuttingMethod> getCuttingMethod(Integer length, List<TestDTO> dtoList, BigDecimal[] widths, Integer materialWidth){
		//给不同宽度的物料进行分类
		String[] widthsStrArray = new String[widths.length];
		for (int j = 0; j < widths.length; j++) {
			widthsStrArray[j] = widths[j].toString();
		}
		
		for (int i = 0; i < widths.length; i++) {
			BigDecimal width = widths[i];
			List<TestDTO> widerList = getWiderDTO(dtoList, width);
			List<List<TestDTO>> cuttingList = getSigleCuttingDTO(widerList, length);
			
		}
		return null;
	}
	
	/**
	 * @Description 找到更宽的料
	 * @param list
	 * @param width
	 * @return
	 * Create at: 2020-09-04 14:56:15
	 * @author: duanyong
	 * Revision:
	 *    2020-09-04 14:56:15 - first revision by duanyong
	 *        
	 */
	private static List<TestDTO> getWiderDTO(List<TestDTO> list, BigDecimal width) {
		List<TestDTO> result = new ArrayList<>();
		Iterator<TestDTO> it = list.iterator();
		while(it.hasNext()) {
			TestDTO dto = it.next();
			if (dto.getWidth().compareTo(width) >= 0) {
				result.add(dto);
			}
		}
		
		return result;
	}
	
	/**
	 * @Description 逐个计算符合该宽度所需要的物料方案
	 * @param list
	 * @param length
	 * @return
	 * Create at: 2020-09-04 15:04:38
	 * @author: duanyong
	 * Revision:
	 *    2020-09-04 15:04:38 - first revision by duanyong
	 *        
	 */
	private static List<List<TestDTO>> getSigleCuttingDTO(List<TestDTO> list, Integer length){
		List<List<TestDTO>> result = new ArrayList<>();
		Collections.sort(list, (t1, t2) -> t2.getNum().compareTo(t1.getNum()));
		List<TestDTO> otherList = new ArrayList<>();
		Iterator<TestDTO> it = list.iterator();
		while (it.hasNext()) {
			TestDTO testDTO = it.next();
			if (testDTO.getNum() - length >= 0) {
				List<TestDTO> tdList = Collections.singletonList(testDTO);
				result.add(tdList);
			} else {
				otherList.add(testDTO);
			}
		}
		if (0 != otherList.size()) {
			int add = 0;
			Map<String, TestDTO> otherMap = new HashMap<>();
			for (TestDTO testDTO : otherList) {
				add = add + testDTO.getNum();
				otherMap.put(testDTO.getId(), testDTO);
			}
			//取出两块或者三块物料可以满足条件的情况
			if (add - length > 0) {
				Map<Integer, List<List<TestDTO>>> combinationMap = new HashMap<>();
				for (int i = 2; i <= otherList.size(); i++) {
					
					if (4 == i) {
						break;
					}
					List<List<TestDTO>> comList = getCombination(otherList, i, otherMap);
					combinationMap.put(i, comList);
				}
			}
		}
		
		return result;
	}
	
	private static List<List<TestDTO>> getCombination(List<TestDTO> list, int num, Map<String, TestDTO> otherMap){
		List<List<TestDTO>> result = new ArrayList<>();
		if (num > list.size()) {
			return null;
		}
		if (num == list.size()) {
			result.add(list);
			return result;
		}
		if (num == 1) {
			for (TestDTO dto : list) {
				List<TestDTO> item = Collections.singletonList(dto);
				result.add(item);
			}
			return result;
		}
		String[] idArray = new String[list.size()];
		for (int i = 0; i < list.size(); i ++) {
			idArray[i] = list.get(i).getId();
		}
		List<String[]> com = MathUtil.arrangementSelect(idArray, num);
		for (String[] strings : com) {
			List<TestDTO> item = new ArrayList<>();
			
			result.add(item);
		}
		
		return result;
	}
	
	public static void main(String[] args) {
//		TestDTO dto1 = TestDTO.builder().num(12).build();
//		TestDTO dto2 = TestDTO.builder().num(11).build();
//		TestDTO dto3 = TestDTO.builder().num(13).build();
//		
//		List<TestDTO> list = new ArrayList<>();
//		list.add(dto1);
//		list.add(dto2);
//		list.add(dto3);
//		Collections.sort(list, (t1, t2) -> t2.getNum().compareTo(t1.getNum()));
//		for (TestDTO testDTO : list) {
//			System.out.println(testDTO.getNum());
//		}
		
//		List<String> dsad = Collections.singletonList("sdljfsdf");
//		System.out.println(dsad);
//		
//		String sd = new String("sdljfsdf");
//		System.out.println(dsad.contains(sd));
		
//		String[] strings = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
//		List<String[]> com = MathUtil.combinationSelect(strings, 2);
//		for (String[] strings2 : com) {
//			System.out.println("----------------------");
//			for (int i = 0; i < strings2.length; i++) {
//				System.out.println(strings2[i]);
//			}
//		}
//		System.out.println("------------------");
//		System.out.println(com.size());
		
		System.out.println(new BigDecimal("28.65").toString());
	}
}
